<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="关键字" prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="用户名/昵称/手机号" clearable class="!w-[200px]"
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="是否返佣" prop="status">
          <el-select v-model="queryParams.status" placeholder="全部" clearable class="!w-[200px]">
            <el-option label="全部" :value="0" />
            <el-option label="未返佣" :value="1" />
            <el-option label="已返佣" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker :editable="false" class="!w-[240px]" v-model="dateCollect" type="daterange"
            range-separator="~" start-placeholder="开始时间" end-placeholder="截止时间" value-format="x" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table v-loading="loading" :data="pageData" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="序号" align="center" type="index" width="80" />
        <el-table-column label="用户名" align="center" prop="name" width="150" />
        <el-table-column label="手机号码" align="center" prop="tel" />
        <el-table-column label="金额" align="center" prop="price" />
        <el-table-column prop="name" label="类型" align="center">
          <template #default="{ row }">
            {{ consumption(row) }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" width="180">
          <template #default="scope">
            <template v-if="scope.row.created_at > 0">
              {{ formatTimestamp(scope.row.created_at * 1000) }}
            </template>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "User",
  inheritAttrs: false,
});

import TradeAPI, { UserPageQuery, UserPageVO } from "@/api/tradeLog";
import { formatTimestamp } from '@/utils/index'

const queryFormRef = ref(ElForm);
const loading = ref(false);
const removeIds = ref([]);
const total = ref(0);
const pageData = ref<UserPageVO[]>();
/** 用户查询参数  */
const queryParams = reactive<UserPageQuery>({
  pageNum: 1,
  pageSize: 10,
  // startDate: "",
  // endDate: ""
});

const consumption = (item: any) => {
  let content = ''
  if (item.is_add == 1) {
    return content = item.is_type == 1 ? '项目库/合伙人发布' : '人脉圈发布'
  } else if (item.is_show == 1) {
    return content = item.is_type == 1 ? '查看项目库/合伙人' : '查看人脉圈'
  } else if (item.is_vip == 1) {
    return content = '充值会员'
  } else if (item.is_withdrawal == 1) {
    return content = '提现'
  } else if (item.is_recharge == 1) {
    return content = '充值余额'
  }

  return content
}


const handleRemarks = (row: { [key: string]: any }) => {
  ElMessageBox.prompt(
    "请输入用户「" + row.name + "」的备注",
    "备注",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      inputType: 'textarea',
    },
  ).then(
    ({ value }) => {
      // TradeAPI.sendRemarks({ id: row.id, remarks: value }).then(() => {
      //   ElMessage.success("操作成功");
      // });
    },
    () => {
      ElMessage.info("已取消");
    }
  );
}
/** 查询 */
const dateCollect = ref<any>([]);
function handleQuery() {
  loading.value = true;
  if (dateCollect.value.length > 0) {
    queryParams.startDate = dateCollect.value[0] / 1000;
    queryParams.endDate = dateCollect.value[1] / 1000;
  } else {
    queryParams.startDate = null;
    queryParams.endDate = null;
  }
  TradeAPI.getConsumPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  dateCollect.value = [];
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

onMounted(() => {
  handleQuery();
});
</script>
